home *** CD-ROM | disk | FTP | other *** search
/ The Utilities Experience / The Utilities Experience - Volume 1.iso / software / misc / o-z / x-windows / mesa-amiwin / demos / xdemo.c < prev   
Encoding:
C/C++ Source or Header  |  1995-12-03  |  6.5 KB  |  306 lines

  1. /* xdemo.c */
  2.  
  3.  
  4. /*
  5.  * Very simple demo of how to use the Mesa/X11 interface instead of the
  6.  * glx, tk or aux toolkits.
  7.  *
  8.  * This program is in the public domain.
  9.  *
  10.  * Brian Paul
  11.  */
  12.  
  13.  
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <X11/Xlib.h>
  19. #include <X11/Xutil.h>
  20. #include "GL/xmesa.h"
  21. #include "GL/gl.h"
  22.  
  23. extern sleep(int);
  24.  
  25.  
  26. static GLint Black, Red, Green, Blue;
  27.  
  28.  
  29.  
  30. static void make_window( char *title, int color_flag )
  31. {
  32.    int x = 10, y = 10, width = 400, height = 300;
  33.    Display *dpy;
  34.    int scr;
  35.    Window root, win;
  36.    Colormap cmap;
  37.    XColor xcolor;
  38.    int attr_flags;
  39.    XVisualInfo *visinfo;
  40.    XSetWindowAttributes attr;
  41.    XTextProperty tp;
  42.    XSizeHints sh;
  43.    XEvent e;
  44.    XMesaContext context;
  45.  
  46.  
  47.    /*
  48.     * Do the usual X things to make a window.
  49.     */
  50.  
  51.    dpy = XOpenDisplay(NULL);
  52.    if (!dpy) {
  53.       printf("Couldn't open default display!\n");
  54.       exit(1);
  55.    }
  56.  
  57.    scr = DefaultScreen(dpy);
  58.    root = RootWindow(dpy, scr);
  59.  
  60.    /* alloc visinfo struct */
  61.    visinfo = (XVisualInfo *) malloc( sizeof(XVisualInfo) );
  62.  
  63.    /* Get a visual and colormap */
  64.    if (color_flag) {
  65.       /* Open TrueColor window */
  66.  
  67. /*
  68.       if (!XMatchVisualInfo( dpy, scr, 24, TrueColor, visinfo )) {
  69.      printf("Couldn't get 24-bit TrueColor visual!\n");
  70.      exit(1);
  71.       }
  72. */
  73.       if (!XMatchVisualInfo( dpy, scr, 8, PseudoColor, visinfo )) {
  74.      printf("Couldn't get 8-bit PseudoColor visual!\n");
  75.      exit(1);
  76.       }
  77.  
  78.       cmap = XCreateColormap( dpy, root, visinfo->visual, AllocNone );
  79.       Black = Red = Green = Blue = 0;
  80.    }
  81.    else {
  82.       /* Open color index window */
  83.  
  84.       if (!XMatchVisualInfo( dpy, scr, 8, PseudoColor, visinfo )) {
  85.      printf("Couldn't get 8-bit PseudoColor visual\n");
  86.      exit(1);
  87.       }
  88.  
  89.       cmap = XCreateColormap( dpy, root, visinfo->visual, AllocNone );
  90.  
  91.       /* Allocate colors */
  92.       xcolor.red   = 0x0;
  93.       xcolor.green = 0x0;
  94.       xcolor.blue  = 0x0;
  95.       xcolor.flags = DoRed | DoGreen | DoBlue;
  96.       if (!XAllocColor( dpy, cmap, &xcolor )) {
  97.      printf("Couldn't allocate black!\n");
  98.      exit(1);
  99.       }
  100.       Black = xcolor.pixel;
  101.  
  102.       xcolor.red   = 0xffff;
  103.       xcolor.green = 0x0;
  104.       xcolor.blue  = 0x0;
  105.       xcolor.flags = DoRed | DoGreen | DoBlue;
  106.       if (!XAllocColor( dpy, cmap, &xcolor )) {
  107.      printf("Couldn't allocate red!\n");
  108.      exit(1);
  109.       }
  110.       Red = xcolor.pixel;
  111.  
  112.       xcolor.red   = 0x0;
  113.       xcolor.green = 0xffff;
  114.       xcolor.blue  = 0x0;
  115.       xcolor.flags = DoRed | DoGreen | DoBlue;
  116.       if (!XAllocColor( dpy, cmap, &xcolor )) {
  117.      printf("Couldn't allocate green!\n");
  118.      exit(1);
  119.       }
  120.       Green = xcolor.pixel;
  121.  
  122.       xcolor.red   = 0x0;
  123.       xcolor.green = 0x0;
  124.       xcolor.blue  = 0xffff;
  125.       xcolor.flags = DoRed | DoGreen | DoBlue;
  126.       if (!XAllocColor( dpy, cmap, &xcolor )) {
  127.      printf("Couldn't allocate blue!\n");
  128.      exit(1);
  129.       }
  130.       Blue = xcolor.pixel;
  131.    }
  132.  
  133.    /* set window attributes */
  134.    attr.colormap = cmap;
  135.    attr.event_mask = ExposureMask | StructureNotifyMask;
  136.    attr.border_pixel = BlackPixel( dpy, scr );
  137.    attr.background_pixel = BlackPixel( dpy, scr );
  138.    attr_flags = CWColormap | CWEventMask | CWBorderPixel | CWBackPixel;
  139.  
  140.    /* Create the window */
  141.    win = XCreateWindow( dpy, root, x,y, width, height, 0,
  142.                 visinfo->depth, InputOutput,
  143.                 visinfo->visual,
  144.                 attr_flags, &attr);
  145.    if (!win) {
  146.       printf("Couldn't open window!\n");
  147.       exit(1);
  148.    }
  149.  
  150.    XStringListToTextProperty(&title, 1, &tp);
  151.    sh.flags = USPosition | USSize;
  152.    XSetWMProperties(dpy, win, &tp, &tp, 0, 0, &sh, 0, 0);
  153.    XMapWindow(dpy, win);
  154.    while (1) {
  155.       XNextEvent( dpy, &e );
  156.       if (e.type == MapNotify && e.xmap.window == win) {
  157.      break;
  158.       }
  159.    }
  160.  
  161.  
  162.    /*
  163.     * Now do the special Mesa/Xlib stuff!
  164.     */
  165.  
  166.    /* Create a Mesa rendering context */
  167.    context = XMesaCreateContext( dpy, visinfo, (GLboolean) color_flag,
  168.                  GL_FALSE, GL_FALSE, NULL );
  169.    if (!context) {
  170.       printf("Couldn't create Mesa/X context!\n");
  171.       exit(1);
  172.    }
  173.  
  174.    XMesaBindWindow( context, win );
  175.    XMesaMakeCurrent( context );
  176.  
  177.    /* Ready to render! */
  178. }
  179.  
  180.  
  181.  
  182. static void draw_cube( void )
  183. {
  184.    /* X faces */
  185.    glIndexi( Red );
  186.    glColor3f( 1.0, 0.0, 0.0 );
  187.    glBegin( GL_POLYGON );
  188.    glVertex3f( 1.0, 1.0, 1.0 );
  189.    glVertex3f( 1.0, -1.0, 1.0 );
  190.    glVertex3f( 1.0, -1.0, -1.0 );
  191.    glVertex3f( 1.0, 1.0, -1.0 );
  192.    glEnd();
  193.  
  194.    glBegin( GL_POLYGON );
  195.    glVertex3f( -1.0, 1.0, 1.0 );
  196.    glVertex3f( -1.0, 1.0, -1.0 );
  197.    glVertex3f( -1.0, -1.0, -1.0 );
  198.    glVertex3f( -1.0, -1.0, 1.0 );
  199.    glEnd();
  200.  
  201.    /* Y faces */
  202.    glIndexi( Green );
  203.    glColor3f( 0.0, 1.0, 0.0 );
  204.    glBegin( GL_POLYGON );
  205.    glVertex3f(  1.0, 1.0,  1.0 );
  206.    glVertex3f(  1.0, 1.0, -1.0 );
  207.    glVertex3f( -1.0, 1.0, -1.0 );
  208.    glVertex3f( -1.0, 1.0,  1.0 );
  209.    glEnd();
  210.  
  211.    glBegin( GL_POLYGON );
  212.    glVertex3f(  1.0, -1.0,  1.0 );
  213.    glVertex3f( -1.0, -1.0,  1.0 );
  214.    glVertex3f( -1.0, -1.0, -1.0 );
  215.    glVertex3f(  1.0, -1.0, -1.0 );
  216.    glEnd();
  217.  
  218.    /* Z faces */
  219.    glIndexi( Blue );
  220.    glColor3f( 0.0, 0.0, 1.0 );
  221.    glBegin( GL_POLYGON );
  222.    glVertex3f(  1.0,  1.0,  1.0 );
  223.    glVertex3f( -1.0,  1.0,  1.0 );
  224.    glVertex3f( -1.0, -1.0,  1.0 );
  225.    glVertex3f(  1.0, -1.0,  1.0 );
  226.    glEnd();
  227.  
  228.    glBegin( GL_POLYGON );
  229.    glVertex3f(  1.0, 1.0, -1.0 );
  230.    glVertex3f(  1.0,-1.0, -1.0 );
  231.    glVertex3f( -1.0,-1.0, -1.0 );
  232.    glVertex3f( -1.0, 1.0, -1.0 );
  233.    glEnd();
  234. }
  235.  
  236.  
  237.  
  238.  
  239. static void display_loop( void )
  240. {
  241.    GLfloat xrot, yrot, zrot;
  242.  
  243.    xrot = yrot = zrot = 0.0;
  244.  
  245.    glClearColor( 0.0, 0.0, 0.0, 0.0 );
  246.    glClearIndex( Black );
  247.  
  248.    glMatrixMode( GL_PROJECTION );
  249.    glLoadIdentity();
  250.    glFrustum( -1.0, 1.0,  -1.0, 1.0,  1.0, 10.0 );
  251.    glTranslatef( 0.0, 0.0, -5.0 );
  252.  
  253.    glMatrixMode( GL_MODELVIEW );
  254.    glLoadIdentity();
  255.  
  256.    glCullFace( GL_BACK );
  257.    glEnable( GL_CULL_FACE );
  258.  
  259.    glShadeModel( GL_FLAT );
  260.  
  261.    while (1) {
  262.       glClear( GL_COLOR_BUFFER_BIT );
  263.       glPushMatrix();
  264.       glRotatef( xrot, 1.0, 0.0, 0.0 );
  265.       glRotatef( yrot, 0.0, 1.0, 0.0 );
  266.       glRotatef( zrot, 0.0, 0.0, 1.0 );
  267.  
  268.       draw_cube();
  269.  
  270.       glPopMatrix();
  271.       glFinish();
  272.  
  273.       sleep(1);  /* slow things down! */
  274.  
  275.       xrot += 10.0;
  276.       yrot += 7.0;
  277.       zrot -= 3.0;
  278.    }
  279.  
  280. }
  281.  
  282.  
  283.  
  284.  
  285. main( int argc, char *argv[] )
  286. {
  287.    if (argc<2) {
  288.       printf("Specify -ci for 8-bit color index or -rgb for RGB mode\n");
  289.       exit(1);
  290.    }
  291.  
  292.    if (strcmp(argv[1],"-ci")==0) {
  293.       make_window( argv[0], 0 );
  294.    }
  295.    else if (strcmp(argv[1],"-rgb")==0) {
  296.       make_window( argv[0], 1 );
  297.    }
  298.    else {
  299.       printf("Bad flag: %s\n", argv[1]);
  300.       exit(1);
  301.    }
  302.  
  303.    display_loop();
  304. }
  305.  
  306.